function s = parse_meta(label_cell,omeMeta)
%% Thomas Stevens - Lim Lab
%opened September 2012
%Extracted from first frame
%Returns structures with fields
%   channels #
%   positions #
%   time #
%   x #pixels
%   y #pixels
%   z #pixels
%   calibX in um/px
%   calibY in um/px
%   calibZ in um/px
%   channel_name (from Nikon Optical Config)
%   channel_name_std -- standardized to 'WRGB'
%   channel_num_std -- standardized to [1,2,3,4] :: [W,R,G,B]
%   t_vector -- starts at t0, increments by delta T in minutes

%channel name parsing: 1=BF (or dic,phase), 2=R, 3=G (or Y/citrine), 4=B (or cerulean)
channel_pattern = '(DIC|BF|Phase)?(RFP|mCherry|Red|561)?(GFP|YFP|Citrine|488)?(CFP|BFP|Cerulean|405|437)?';
channel_std_list = 'WRGB?';

s = struct();

file_path = label_cell{1};
file_pattern = '/.*nd2';
s.path = char(regexp(file_path,file_pattern,'match'));

s.positions = double(omeMeta.getImageCount());

s.dim_order = char(omeMeta.getPixelsDimensionOrder(0));

s.x = omeMeta.getPixelsSizeX(0).getValue();
s.y = omeMeta.getPixelsSizeY(0).getValue();
if ~isempty(omeMeta.getPixelsSizeZ(0))
    s.z = omeMeta.getPixelsSizeZ(0).getValue();
end

s.calibX = omeMeta.getPixelsPhysicalSizeX(0).getValue();
s.calibY = omeMeta.getPixelsPhysicalSizeY(0).getValue();
if ~isempty(omeMeta.getPixelsPhysicalSizeZ(0))
    s.calibZ = omeMeta.getPixelsPhysicalSizeZ(0).getValue();
else
    s.calibZ = 1;
end

if ~isempty(omeMeta.getPixelsSizeC(0))
    s.channels = omeMeta.getPixelsSizeC(0).getValue();
end
if ~isempty(omeMeta.getPixelsSizeT(0))
    s.time = omeMeta.getPixelsSizeT(0).getValue();
end

s.channel_name = cell(1,s.channels);
s.channel_name_std = char(1,s.channels);
s.channel_num_std = [];
for i=1:s.channels
    s.channel_name{i} = char(omeMeta.getChannelName(0,i-1));
    tokens = regexpi(s.channel_name{i},channel_pattern,'tokens');
    if isempty(tokens)
        'Channel Name is not recognized.'
        return
    else
        s.channel_num_std(i) = find(~cellfun(@isempty,tokens{:}));
        s.channel_name_std(i) = channel_std_list(s.channel_num_std(i));
    end
end
